﻿using AutoMapper;
using ManagementApi.Authorizers;
using ManagementApi.Models;
using ManagementApi.Models.Manage.ProjectGroupMember;
using ManagementApi.Models.Person;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Mvc.Filters;
using ManagementApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.Corp;
using Modules.Device;
using Modules.Device.Entitys;
using Modules.Person;
using Modules.Project;
using Modules.Project.Entitys;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using PmSoft;
using PmSoft.Entity;
using PmSoft.Exceptions;
using static Modules.Device.CheckInDeviceConstants;
using static Modules.Person.PersonConstants;

namespace ManagementApi.Controllers.CorpPannel
{
    /// <summary>
    /// 花名册管理接口
    /// </summary>
    [Route("/api/v1/corp/useemployee")]
    [ApiController]
    [Authorize(Roles = "Corp")]
    [TypeFilter(typeof(RequireSelProjectActionFilter))]
    public class UseEmployeeController : ControllerBase
    {
        private readonly ProjectGroupMemberService projectGroupMemberService;
        private readonly ProjectService projectService;
        private readonly ProjectGroupService projectGroupService;
        private readonly CorpService corpService;
        private readonly PersonService personService;
        private readonly PersonCorpContractService personCorpContractService;
        private readonly PublicPersonService publicPersonService;
        private readonly CorpApplicationContext applicationContext;
        private readonly CheckinDeviceService projectCheckinDeviceService;
        private readonly CheckinDeviceCommandService projectCheckinDeviceCommandService;
        private readonly PersonBankService personBankService;
        private readonly IMapper mapper;
        private readonly ExportService exportService;
        private readonly ProjectOwnAuthorizer ownAuthorizer;

        /// <summary>
        /// 当前登录企业参建单位
        /// </summary>
        private readonly 项目参建单位类型 corpRole;

        public UseEmployeeController(
            ProjectGroupMemberService projectGroupMemberService,
            ProjectService projectService,
            ProjectGroupService projectGroupService,
            CorpService corpService,
            PersonService personService,
            PersonCorpContractService personCorpContractService,
            PublicPersonService publicPersonService,
            CorpApplicationContext applicationContext,
            CheckinDeviceService projectCheckinDeviceService,
            CheckinDeviceCommandService projectCheckinDeviceCommandService,
            PersonBankService personBankService,
            IMapper mapper, ExportService exportService,
            ProjectOwnAuthorizer ownAuthorizer
            )
        {
            this.projectGroupMemberService = projectGroupMemberService;
            this.projectService = projectService;
            this.projectGroupService = projectGroupService;
            this.corpService = corpService;
            this.personService = personService;
            this.personCorpContractService = personCorpContractService;
            this.publicPersonService = publicPersonService;
            this.applicationContext = applicationContext;
            this.projectCheckinDeviceService = projectCheckinDeviceService;
            this.projectCheckinDeviceCommandService = projectCheckinDeviceCommandService;
            this.mapper = mapper;
            this.personBankService = personBankService;
            this.ownAuthorizer = ownAuthorizer;
            this.exportService = exportService;
        }

        /// <summary>
        /// 现场管理人员（分页）
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page_managerEmployees")]
        public ApiResult<PageDto<UseEmployeeDto>> GetUseManagerEmployees([FromQuery] ProjectGroupMemberQuery query, int pageIndex = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            if (!applicationContext.IsLumpContractorCorp())
                query.CorpId = applicationContext.CurrentUser?.Id;
            query.IsManager = true;
            var data = projectGroupMemberService.GetProjectGroupMembersByManager(query, pageIndex, pageSize);
            var corps = corpService.GetCorpsByCorpIds(data.Select(a => a.CorpId));
            var projects = projectService.GetProjectInfoByProjectIds(data.Select(a => a.ProjectId));
            var persons = personService.GetPersons(data.Select(a => a.PersonId));
            var personCorpContracts = personCorpContractService.GetPersonCorpContractsByProjectIds(data.Select(a => a.ProjectId));
            var dtos = data.AsPageDto(source => { return source.AsManagerEmployeeDto(corps, projects, persons, personCorpContracts); });
            return ApiResult<PageDto<UseEmployeeDto>>.Success(dtos);
        }

        /// <summary>
        /// 用工工人信息（分页）
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page_useEmployees")]
        public ApiResult<PageDto<UseEmployeeDto>> GetUseEmployees([FromQuery] ProjectGroupMemberQuery query, int pageIndex = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            if (!applicationContext.IsLumpContractorCorp())
                query.CorpId = applicationContext.CurrentUser?.Id;
            var data = projectGroupMemberService.GetProjectGroupMembers(query, pageIndex, pageSize);
            var corps = corpService.GetCorpsByCorpIds(data.Select(a => a.CorpId));
            var projects = projectService.GetProjectInfoByProjectIds(data.Select(a => a.ProjectId));
            var groups = projectGroupService.GetProjectGroups(data.Select(a => a.GroupId));
            var persons = personService.GetPersons(data.Select(a => a.PersonId));
            var personCorpContracts = personCorpContractService.GetPersonCorpContractsByProjectIds(data.Select(a => a.ProjectId));
            var dtos = data.AsPageDto(source => { return source.AsUseEmployeeDto(corps, projects, groups, persons, personCorpContracts); });
            return ApiResult<PageDto<UseEmployeeDto>>.Success(dtos);
        }

        /// 导出花名册Excel </summary> <returns></returns>
        [HttpGet("export")]
        public IActionResult Export([FromQuery] ProjectGroupMemberQuery query, int pageIndex = 1, int pageSize = 9999)
        {
            if (!query.ProjectId.HasValue) query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            if (!applicationContext.IsLumpContractorCorp())
                query.CorpId = applicationContext.CurrentUser?.Id;
            var data = projectGroupMemberService.GetProjectGroupMembers(query, pageIndex, pageSize);

            var projectInfo = projectService.GetProjectInfo(query.ProjectId ?? 0) ?? throw new BusinessException("项目不存在");

            List<UseEmployeeExportDto> exportDtos = data.AsUseEmployeeExportDtos().ToList();
            // 创建一个新的Excel工作簿
            IWorkbook workbook = new XSSFWorkbook();

            // 创建一个新的工作表
            ISheet sheet = workbook.CreateSheet("Sheet1");

            #region 表头

            // 合并行和列的方式设置单元格的边界
            CellRangeAddress left = new CellRangeAddress(0, 1, 0, 10);
            CellRangeAddress right = new CellRangeAddress(0, 1, 11, 17);
            sheet.AddMergedRegion(left);
            sheet.AddMergedRegion(right);
            // 创建一个新的行
            IRow row = sheet.CreateRow(0);
            ICell cell = row.CreateCell(0);
            ICell cell2 = row.CreateCell(11);
            // 创建字体对象
            IFont font = workbook.CreateFont();
            font.FontHeightInPoints = 20; // 设置字体大小为 12

            // 设置单元格样式
            ICellStyle cellStyle = workbook.CreateCellStyle();
            cellStyle.Alignment = HorizontalAlignment.Center;//居中
            cellStyle.SetFont(font);
            cell.CellStyle = cellStyle;
            cell2.CellStyle = cellStyle;
            // 设置单元格的值
            cell.SetCellValue(projectInfo.Name);
            cell2.SetCellValue(projectInfo.PrjNum);

            int index = 1;

            #endregion 表头

            index++;
            exportService.CellRange(sheet, workbook, index, 0, 0, "姓名", "性别", "身份证号码", "联系电话", "家庭地址", "籍贯", "发卡银行", "银行卡号", "参建单位", "班组名称", "工种类别", "是否项目管理员", "班组进场时间", "班组离场时间", "是否班组长", "是否签订劳动合同", "人员进场日期", "人员离场日期");

            #region 数据部分

            foreach (var item in exportDtos)
            {
                index++;
                exportService.CellRange(sheet, workbook, index, 0, 0, item.PersonName, item.Gender, item.CardNumber, item.Mobile, item.Address, item.Nation, item.BankName, item.BankCardNo, item.CorpName, item.GroupName, item.TypeName, item.IsManager, item.GroupStartDate, item.GroupEndDate, item.IsLeader, item.IsContract, item.PersonStartDate, item.PersonEndDate);
            }
            // 创建列并设置最小宽度
            int columnWidth = 20;
            int columnIndex = 0;
            /*  sheet.SetColumnWidth(columnIndex, columnWidth * 200);*/ // 设定列宽度，参数为1/256个字符宽度
                                                                        // 自动调整列宽
            for (int i = 0; i < 18; i++)
            {
                // 自动调整列宽
                sheet.AutoSizeColumn(i);
                // sheet.SetColumnWidth(i, Math.Max(columnWidth * 200, sheet.GetColumnWidth(i)));
            }

            #endregion 数据部分

            // 将工作簿保存到内存流中
            using (MemoryStream memoryStream = new MemoryStream())
            {
                workbook.Write(memoryStream);
                // 返回文件内容结果
                return File(memoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"花名册信息表_{DateTime.Now.ToString("yyyyMMddHHmmss ")}.xlsx");
            }
        }

        /// <summary>
        /// 人员信息登记
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("registerEmployee")]
        public ApiResult<ProjectGroupMemberDto> CreatePersonGroupMember(PersonGroupMemberCreateForm form)
        {
            ProjectInfo? projectInfo = projectService.GetProjectInfo(applicationContext.CurrentProjectInfo.ProjectId);
            if (projectInfo == null) return ApiResult<ProjectGroupMemberDto>.Failure("项目信息不存在");
            int personId = publicPersonService.SavePersonReadCard(form.Person);
            if (form.Person == null) return ApiResult<ProjectGroupMemberDto>.Failure("人员信息不存在");
            if (form.Member == null) return ApiResult<ProjectGroupMemberDto>.Failure("人员岗位信息不存在");
            PersonInfo? person = personService.GetPerson(personId);
            if (person == null) return ApiResult<ProjectGroupMemberDto>.Failure("人员信息不存在");
            form.Member.PersonId = personId;

            mapper.Map(form.Person, person);
            ProjectGroupMember member = new();
            mapper.Map(form.Member, member);

            ProjectGroup? projectGroup = projectGroupService.GetProjectGroup(form.Member.GroupId);
            if (projectGroup == null) return ApiResult<ProjectGroupMemberDto>.Failure("班组信息不存在");
            member.CorpId = projectGroup.CorpId;
            if (projectGroup.StartDate > form.Member.StartDate)
                return ApiResult<ProjectGroupMemberDto>.Failure("进场时间不能小于:" + projectGroup.StartDate);
            if (form.Member.Role == 工人类型.建筑工人)
            {

                #region 验证数据权限

                ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(projectGroup.ProjectId);

                #endregion 验证数据权限

                member.IsManager = false;
            }
            else if (form.Member.Role == 工人类型.管理人员)
            {
                if (projectInfo.IndustryType == 行业主管部门.住建)
                {
                    if (form.Member.PostCategory == 管理岗位类型.项目经理 || form.Member.PostCategory == 管理岗位类型.安全员 || form.Member.PostCategory == 管理岗位类型.技术负责人 || form.Member.PostCategory == 管理岗位类型.总监理工程师)
                        return ApiResult<ProjectGroupMemberDto>.Failure($"对不起，住建项目不允许手动新增关键岗位管理人员");
                }

                #region 验证数据权限

                ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(projectGroup.ProjectId);

                #endregion 验证数据权限

                //查询当前班组下是否有当前管理员
                ProjectGroupMember? projectGroupMember = projectGroupMemberService.GetProjectGroupMemberByGroupPerson(member.GroupId, personId);
                if (projectGroupMember != null && projectGroupMember.IsManager)
                    return ApiResult<ProjectGroupMemberDto>.Failure($"人员{person.Name} 已在当前参建单位现场管理人员中");
                member.IsManager = true;
            }
            else return ApiResult<ProjectGroupMemberDto>.Failure($"人员类型错误");

            member.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            member.CreateTime = DateTime.Now;
            member.Status = 进出场状态.进场;
            #region 证书

            List<PersonCertificate> certificates = new();
            List<PersonAttachment> attachs = new();
            List<PersonCertificateSpeciality> specials = new();
            foreach (var item in form.certificates)
            {
                PersonCertificate personCertificate = mapper.Map<PersonCertificate>(item);
                personCertificate.Id = Guid.NewGuid().ToString().Replace("-", "");
                personCertificate.PersonId = person.PersonId;
                certificates.Add(personCertificate);

                #region 专业

                if (item.Speciality != null)
                {
                    foreach (var special in item.Speciality)
                    {
                        PersonCertificateSpeciality speciality = new()
                        {
                            Id = Guid.NewGuid().ToString().Replace("-", ""),
                            CetificteId = personCertificate.Id,
                        };
                        mapper.Map(special, speciality);
                        specials.Add(speciality);
                    }
                }

                #endregion 专业

                #region 附件

                if (item.Attachments != null)
                {
                    foreach (var attach in item.Attachments)
                    {
                        PersonAttachment personAttachment = new()
                        {
                            AttachmentId = attach.AttachmentId,
                            Tenant = 人员附件租户.Certificate,
                            ObjectId = personCertificate.Id,
                            AttachmentType = 人员附件类型.证书附件,
                            CreateTime = DateTime.Now
                        };
                        attachs.Add(personAttachment);
                    }
                }

                #endregion 附件
            }

            #endregion 证书

            personService.CreatePersonUpdateMember(person, member, form.Bank.CardNo, form.Bank.BankName, certificates, attachs, specials);

            #region 下发人员信息到设备

            IEnumerable<CheckinDevice> devices = projectCheckinDeviceService.GetProjectCheckinDevicesByProjectId(applicationContext.CurrentProjectInfo.ProjectId);
            foreach (var device in devices)
            {
                CheckinDeviceCommand log = new CheckinDeviceCommand()
                {
                    Name = 考勤机命令.人员进场后下发.GetDescription(),
                    Code = 考勤机命令.人员进场后下发,
                    MessageId = Guid.NewGuid().ToString(),
                    DeviceCode = device.Code,
                    Status = 命令下发状态.待下发,
                    IsAuto = true
                };
                List<PersonInfo> personInfos = new List<PersonInfo>() { person };
                projectCheckinDeviceCommandService.Send(log, device, personInfos);
            }

            #endregion 下发人员信息到设备

            return ApiResult<ProjectGroupMemberDto>.Success(member.AsProjectGroupMemberDto());
        }

        /// <summary>
        /// 根据班组查询当前需要录入的人员是否为班组长
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [HttpGet("isLeader/{groupId}")]
        public ApiResult<bool> IsLeader(int groupId)
        {
            var members = projectGroupMemberService.GetProjectGroupMembersByGroupId(groupId);
            if (!members.Any()) return ApiResult<bool>.Success(false);
            return ApiResult<bool>.Success(true);
        }

        /// <summary>
        /// 人员信息编辑
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("editEmployee/{id}")]
        public ApiResult EditPersonGroupMember(int id, PersonGroupMemberEditForm form)
        {
            form.Member.id = id;
            if (form.Member == null) return ApiResult.Failure("班组信息不存在");
            PersonInfo? person = personService.GetPerson((int)form.Person.PersonId);
            mapper.Map(form.Person, person);
            if (person == null) return ApiResult.Failure("人员信息不存在");
            form.Member.PersonId = person.PersonId;

            mapper.Map(form.Person, person);
            ProjectGroupMember member = new();
            mapper.Map(form.Member, member);
            if (form.Member.Role == 工人类型.建筑工人)
            {
                ProjectGroup projectGroup = projectGroupService.GetProjectGroup(form.Member.GroupId) ?? throw new BusinessException("班组不存在");

                #region 验证数据权限

                ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(projectGroup.ProjectId);

                #endregion 验证数据权限

                if (projectGroup.StartDate > form.Member.StartDate)
                    return ApiResult.Failure("进场时间不能小于:" + projectGroup.StartDate);
            }
            else if (form.Member.Role == 工人类型.管理人员)
            {
                //查询当前参建单位管理组
                ProjectGroup? projectGroup = projectGroupService.GetProjectGroupsByProjectId(applicationContext.CurrentProjectInfo.ProjectId).FirstOrDefault(a => a.CorpId == form.Member.CorpId && a.Name == "管理组");
                if (projectGroup != null)
                {
                    #region 验证数据权限

                    ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(projectGroup.ProjectId);

                    #endregion 验证数据权限

                    member.GroupId = projectGroup.GroupId;
                }

                //查询当前班组下是否有当前管理员
                ProjectGroupMember? projectGroupMember = projectGroupMemberService.GetProjectGroupMemberByGroupPerson(member.GroupId, person.PersonId);
                if (projectGroupMember != null && projectGroupMember.Id != form.Member.id && projectGroupMember.IsManager)
                    return ApiResult.Failure($"人员{person.Name} 已在当前参建单位现场管理人员中");
                member.IsManager = true;
            }
            else return ApiResult.Failure($"人员类型错误");

            member.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;

            #region 证书

            List<PersonCertificate> certificates = new();
            List<PersonAttachment> attachs = new();
            List<PersonCertificateSpeciality> specials = new();
            foreach (var item in form.certificates)
            {
                PersonCertificate personCertificate = mapper.Map<PersonCertificate>(item);
                personCertificate.Id = Guid.NewGuid().ToString().Replace("-", "");
                personCertificate.PersonId = person.PersonId;
                certificates.Add(personCertificate);

                #region 专业

                if (item.Speciality != null)
                {
                    foreach (var special in item.Speciality)
                    {
                        PersonCertificateSpeciality speciality = new()
                        {
                            Id = Guid.NewGuid().ToString().Replace("-", ""),
                            CetificteId = personCertificate.Id,
                        };
                        mapper.Map(special, speciality);
                        specials.Add(speciality);
                    }
                }

                #endregion 专业

                #region 附件

                if (item.Attachments != null)
                {
                    foreach (var attach in item.Attachments)
                    {
                        PersonAttachment personAttachment = new()
                        {
                            AttachmentId = attach.AttachmentId,
                            Tenant = 人员附件租户.Certificate,
                            ObjectId = personCertificate.Id,
                            AttachmentType = 人员附件类型.证书附件,
                            CreateTime = DateTime.Now
                        };
                        attachs.Add(personAttachment);
                    }
                }

                #endregion 附件
            }

            #endregion 证书

            personService.CreatePersonUpdateMember(person, member, form.Bank.CardNo, form.Bank.BankName, certificates, attachs, specials);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 根据成员id查询人员信息详情
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        [HttpGet("member/{memberId}")]
        public ApiResult<PersonGroupMemberDto> GetMember(int memberId)
        {
            ProjectGroupMember? member = projectGroupMemberService.GetMember(memberId);
            if (member == null) return ApiResult<PersonGroupMemberDto>.Failure("成员信息不存在");
            return ApiResult<PersonGroupMemberDto>.Success(member.AsPersonDto());
        }

        /// <summary>
        /// 更换班组长
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        [HttpPut("editMemberLeader/{memberId}")]
        public ApiResult EditMemberLeader(int memberId)
        {
            ProjectGroupMember? member = projectGroupMemberService.GetMember(memberId);
            if (member == null)
                return ApiResult.Failure("班组信息不存在");

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(member.ProjectId);

            #endregion 验证数据权限

            //查询当前班组下有没有班组长
            ProjectGroupMember? leaderMember = projectGroupMemberService.GetProjectGroupMembersByProjectId(member.ProjectId).FirstOrDefault(a => a.CorpId == member.CorpId && a.GroupId == member.GroupId && a.IsLeader == true);
            if (leaderMember != null)
            {
                leaderMember.IsLeader = false;
                projectGroupMemberService.Update(leaderMember);
            }
            member.IsLeader = true;
            projectGroupMemberService.Update(member);

            return ApiResult.Success("班组长更换成功");
        }

        /// <summary>
        /// 解绑
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        [HttpPut("unbind/{memberId}")]
        public ApiResult Unbind(int memberId)
        {
            ProjectGroupMember? member = projectGroupMemberService.GetMember(memberId);
            if (member == null)
                return ApiResult.Failure("成员信息不存在");

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(member.ProjectId);

            #endregion 验证数据权限

            ProjectInfo? projectInfo = projectService.GetProjectInfo(member.ProjectId);
            if (projectInfo == null)
                return ApiResult.Failure("项目信息不存在");
            projectInfo.ManagerId = null;
            projectGroupMemberService.Unbind(projectInfo, member);
            return ApiResult.Success("解绑成功");
        }

        /// <summary>
        /// 将当前人员转入管理组
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPut("transferManager/{memberId}")]
        public ApiResult TransferManager(int memberId, ProjectGroupMemberEditForm form)
        {
            ProjectGroupMember? oldmember = projectGroupMemberService.GetMember(memberId);
            if (oldmember == null)
                return ApiResult.Failure("成员信息不存在");

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(oldmember.ProjectId);

            #endregion 验证数据权限

            ProjectGroupMember? newMember = projectGroupMemberService.GetProjectGroupMembersByPersonId(oldmember.PersonId).FirstOrDefault(a => a.ProjectId == oldmember.ProjectId && a.CorpId == form.CorpId && a.IsManager == true);
            if (newMember != null) return ApiResult.Failure($"当前成员已经是现场管理人员，无需重复设置");
            newMember = oldmember;
            mapper.Map(oldmember, newMember);
            newMember.StartDate = form.StartDate;
            newMember.EndDate = form.EndDate;
            newMember.Id = 0;
            newMember.IsManager = true;
            newMember.PostCategory = form.PostCategory;
            newMember.CreateTime = DateTime.Now;
            newMember.IsLeader = false;

            projectGroupMemberService.TransferManager(oldmember, newMember);

            return ApiResult.Success("转入管理组成功");
        }

        /// <summary>
        /// 根据企业id选择人员
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page_useEmployees_byCorpId")]
        public ApiResult<PageDto<PersonInfoDto>> GetUseEmployeesByCorpId([FromQuery] ProjectGroupMemberQuery query, int pageIndex = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            if (!applicationContext.IsLumpContractorCorp())
                query.CorpId = applicationContext.CurrentUser?.Id;
            var data = personService.GetPersonInfosByProjectIdAndCorpId(query.Keywords, query.ProjectId, query.CorpId, pageIndex, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<PersonInfoDto>>.Success(dtos);
        }
    }
}